home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / Caml Light 0.7 / Caml Light 0.7 source / src / appli / ui.c < prev    next >
C/C++ Source or Header  |  1995-07-04  |  5KB  |  224 lines

  1. #include <Dialogs.h>
  2. #include <Events.h>
  3. #include <Files.h>
  4. #include <LowMem.h>
  5. #include <Memory.h>
  6. #include <Resources.h>
  7. #include <Stdlib.h>
  8. #include <Types.h>
  9.  
  10. #include "gui.h"
  11. #include "ui.h"
  12.  
  13. int ui_read (int fd, char * p, unsigned long n)
  14. {
  15.   if (fd == 0){
  16.     return DoRead (p, n);
  17.   }else{
  18.     return read (fd, p, n);
  19.   }
  20. }
  21.  
  22. int ui_write (int fd, char * p, unsigned long n)
  23. {
  24.   if (fd == 1 || fd == 2) {
  25.     DoWrite (CAMLPrinter, p, n);
  26.     return n;
  27.   }else{
  28.     return write (fd, p, n);
  29.   }
  30. }
  31.  
  32. void ui_gc_message (char *fmt, unsigned long arg)
  33. {
  34.   char buf [100];        /* arrgh !! */
  35.   
  36.   sprintf (buf, fmt, arg);
  37.   ui_write (2, buf, strlen (buf));
  38. }
  39.  
  40. void ui_exit (int status)
  41. {
  42.   if (status != 0){
  43.     NoteAlert(140, 0);
  44.     stop_working ();
  45.     while (1) LookEvent (15);
  46.   }else{
  47.      Quit ();
  48.   }
  49. }
  50.  
  51. void ui_fatal_error (char *fmt, char *arg)
  52. {
  53.   char buf [200];         /* arrgh !!! */
  54.  
  55.   sprintf (buf, fmt, arg);
  56.   ui_write (2, buf, strlen (buf));
  57.   ui_exit (3);
  58. }
  59.  
  60. static int maxcount = 30;
  61. static int count = 30;
  62. static unsigned long lastdate = 0;
  63.  
  64. void ui_periodic_action (void)
  65. {
  66.   --count;
  67.   if (count <= 0){
  68.     unsigned long date;
  69.     LookEvent (0);
  70.     date = TickCount ();
  71.     if (date - lastdate < 15 && maxcount < 50) ++maxcount;
  72.     if (date - lastdate > 15 && maxcount > 10) --maxcount;
  73.     count = maxcount;
  74.   }
  75. }
  76.  
  77. char *get_wd_name (short wd_refnum)
  78. {
  79.   char buf [64];
  80.   char *temp, *result, *postfix = ":";
  81.   DirInfo cinfo;
  82.   WDPBRec wd;
  83.  
  84.   wd.ioCompletion = NULL;
  85.   wd.ioNamePtr = NULL;
  86.   wd.ioVRefNum = wd_refnum;
  87.   wd.ioWDIndex = 0;
  88.   wd.ioWDProcID = 0;
  89.   wd.ioWDVRefNum = 0;
  90.   if (PBGetWDInfo (&wd, 0) != noErr) return postfix;
  91.   cinfo.ioDrParID = wd.ioWDDirID;
  92.   result = malloc (strlen (postfix) + 1);
  93.   if (result == NULL) Error (1);
  94.   strcpy (result, postfix);
  95.   do{
  96.     cinfo.ioCompletion = NULL;
  97.     cinfo.ioNamePtr = (StringPtr) buf;
  98.     cinfo.ioVRefNum = wd.ioWDVRefNum;
  99.     cinfo.ioFDirIndex = -1;
  100.     cinfo.ioDrDirID = cinfo.ioDrParID;
  101.     if (PBGetCatInfo ((CInfoPBPtr) &cinfo, 0) != noErr) return postfix;
  102.     p2cstr (buf);
  103.     temp = malloc (strlen (result) + strlen (buf) + 2);
  104.     if (temp == NULL){
  105.       free (result);
  106.       return postfix;
  107.     }
  108.     sprintf (temp, ":%s%s", buf, result);
  109.     free (result);
  110.     result = temp;
  111.   }while (cinfo.ioDrDirID != 2);
  112.   return result + 1;
  113. }
  114.  
  115. char *ui_getenv (char *name)
  116. {
  117.   Handle h;
  118.  
  119.   h = GetNamedResource ('ENV ', (ConstStr255Param) name);
  120.   if (h == nil) return NULL;
  121.   HLock (h);
  122.   return *h;
  123. }
  124.  
  125. int main (int argc, char *argv [])
  126. {
  127. #pragma unused (argc, argv)
  128.   int i;
  129.   int new_argc;
  130.   char *new_argv [6];
  131.   WDPBRec wd;
  132.   char *cur_dir = ":";
  133.   StringPtr curapname;
  134.   Handle h;
  135.  
  136.   Initialize ();
  137.   wd.ioCompletion = NULL;
  138.   wd.ioNamePtr = NULL;
  139.   if (PBHGetVol (&wd, 0) == noErr){
  140.     cur_dir = get_wd_name (wd.ioVRefNum);
  141.   }
  142.   curapname = LMGetCurApName ();
  143.   new_argv [0] = malloc (curapname [0] + 1);
  144.   if (new_argv [0] == NULL) Error (1);
  145.   for (i = 0; i < curapname [0]; i++){
  146.     new_argv [0] [i] = curapname [i+1];
  147.   }
  148.   new_argv [0] [i] = '\0';
  149.  
  150.   new_argv [1] = "-stdlib";
  151.   
  152.   new_argv [2] = malloc (strlen (cur_dir) + 5);
  153.   if (new_argv [2] == NULL) Error (1);
  154.   sprintf (new_argv [2], "%slib:", cur_dir);
  155.  
  156.   h = GetNamedResource ('ENV ', (ConstStr255Param) "\pLANG");
  157.   if (h == nil){
  158.     new_argc = 3;
  159.   }else{
  160.     HLock (h);
  161.     new_argv [3] = "-lang";
  162.     new_argv [4] = *h;
  163.     new_argc = 5;
  164.   }
  165.  
  166.   new_argv [new_argc] = NULL;
  167.   return caml_main (new_argc, new_argv);
  168. }
  169.  
  170. #ifdef __MWERKS__
  171. /* Stubs for the console package.  The calls for stdin, stdout, and stderr
  172.    are already intercepted by ui_write, ui_read, etc, thus the stubs will
  173.    never be called.
  174.    These stubs are adapted from "console.stubs.c" from CodeWarrior 6.
  175. */
  176. #ifndef __CONSOLE__
  177. #include <console.h>
  178. #endif
  179.  
  180. short InstallConsole (short fd)
  181. {
  182. #pragma unused (fd)
  183. #ifdef DEBUG
  184.   DebugStr ("\pUnexpected call to InstallConsole");
  185. #endif
  186.   return 0;
  187. }
  188.  
  189. void RemoveConsole (void)
  190. {
  191. #ifdef DEBUG
  192.   DebugStr ("\pUnexpected call to RemoveConsole");
  193. #endif
  194. }
  195.  
  196. long WriteCharsToConsole (char *buffer, long n)
  197. {
  198. #pragma unused (buffer, n)
  199. #ifdef DEBUG
  200.   DebugStr ("\pUnexpected call to WriteCharsToConsole");
  201. #endif
  202.   return 0;
  203. }
  204.  
  205. long ReadCharsFromConsole (char *buffer, long n)
  206. {
  207. #pragma unused (buffer, n)
  208. #ifdef DEBUG
  209.   DebugStr ("\pUnexpected call to ReadCharsFromConsole");
  210. #endif
  211.     return 0;
  212. }
  213.  
  214. char *__ttyname (long fildes)
  215. {
  216. #pragma unused (fildes)
  217. #ifdef DEBUG
  218.   DebugStr ("\pUnexpected call to __ttyname");
  219. #endif
  220.   return NULL;
  221. }
  222.  
  223. #endif /* __MWERKS__ */
  224.